package zhoujunhao.rbac.service.array;

import zhoujunhao.rbac.domian.dto.UserDto;
import zhoujunhao.rbac.domian.entity.Dept;
import zhoujunhao.rbac.domian.entity.Role;
import zhoujunhao.rbac.domian.entity.User;
import zhoujunhao.rbac.domian.vo.Result;
import zhoujunhao.rbac.domian.vo.UserVo;
import zhoujunhao.rbac.service.IDeptService;
import zhoujunhao.rbac.service.IRoleService;
import zhoujunhao.rbac.service.IUserService;

/**
 * 业务类-用户
 *
 * @author 周均壕
 * @since 2025/6/16 16:04
 */
public class UserService extends BaseService<User, UserDto, UserVo> implements IUserService {


    private static IUserService singleton;
    private final IDeptService deptService;
    private final IRoleService roleService;

    private UserService() {
        deptService = DeptService.getInstance();
        roleService = RoleService.getInstance();
        date = new User[DATA_SIZE];
        temp = new UserVo[DATA_SIZE];
    }

    public static IUserService getInstance() {
        if (singleton == null) {
            singleton = new UserService();
        }
        return singleton;
    }

    @Override
    UserVo convertToVo(User user) {
        String deptName = "";
        Dept dept = deptService.findById(user.deptId);
        if (dept != null) {
            deptName = dept.name;
        }

        String roleName = "";
        Role role = roleService.findById(user.roleId);
        if (role != null) {
            roleName = role.name;
        }

        return new UserVo(user.getId(), user.username, user.name, user.moblie, user.email,
                user.deptId, deptName, user.roleId, roleName, user.enabled);
    }

    @Override
    User createEntity(UserDto dto, int id) {
        return new User(id, dto.username, dto.name, dto.moblie, dto.email, dto.deptId, dto.roleId);
    }

    @Override
    void updateEntity(UserDto dto, User user) {
        user.username = dto.username;
        user.name = dto.name;
        user.moblie = dto.moblie;
        user.email = dto.email;
        user.deptId = dto.deptId;
        user.roleId = dto.roleId;
    }

    @Override
    public boolean deptInUse(int deptid) {
        boolean inUse = false;
        for (User user : date) {
            if (user != null && user.deptId == deptid) {
                inUse = true;
                break;
            }
        }
        return inUse;
    }

    @Override
    public boolean roleInUse(int roleid) {
        boolean inUse = false;
        for (User user : date) {
            if (user != null && user.roleId == roleid) {
                inUse = true;
                break;
            }
        }
        return inUse;
    }

    @Override
    public Result create(UserDto dto) {

        if (!deptService.existsByid(dto.deptId)) {
            return Result.failure("部门不存在");
        }
        if (!roleService.existsByid(dto.roleId)) {
            return Result.failure("角色不存在");
        }
        return super.create(dto);

    }

    @Override
    public Result update(UserDto dto, int id) {

        if (!deptService.existsByid(dto.deptId)) {
            return Result.failure("部门不存在");
        }
        if (!roleService.existsByid(dto.roleId)) {
            return Result.failure("角色不存在");
        }
        return super.update(dto, id);
    }

    @Override
    public Result toggle(int id) {
        if (!existsByid(id)) {
            return Result.failure(" 切换状态失败失败，用户不存在");
        }
        date[id].enabled = !date[id].enabled;
        return Result.success(date[id].enabled ? "启用成功" : "停用成功", null);
    }

}
